package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.*;
import com.cn.medicalcare.pojo.*;
import com.cn.medicalcare.service.NewsService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class NewsServiceImpl implements NewsService {

    public static final String HOME_NEWS_CACHE_KEY = "survival_home_news";

    public static final String NEWS_PAGE_CACHE_KEY = "news_page_by_cate_id_";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final NewsServiceImpl instance = new NewsServiceImpl();
    }

    private NewsServiceImpl() {
    }

    public static NewsServiceImpl getInstance() {
        return NewsServiceImpl.SingletonClassInstance.instance;
    }

    public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public List<News> selectNewsInPage(AllPage<News> allPage) {
        List<News> list = SqlSessionFactoryTool.DoMapper(NewsMapper.class, m -> m.selectNewsInPage(allPage));
        return list;
    }

    @Override
    public int selectNewsCount(News news) {
        return SqlSessionFactoryTool.DoMapper(NewsMapper.class, m -> m.selectNewsCount(news));
    }


    @Override
    public boolean insertNews(News news) {
        news.setIstopupdatetime(new Date());
        boolean insertFlag = SqlSessionFactoryTool.DoMapperAndCommit(NewsMapper.class, m -> m.insertNews(news));
        if (insertFlag) {
            JedisUtil.jedisDo(j -> j.del(HOME_NEWS_CACHE_KEY));
            JedisUtil.deleteKeys(NEWS_PAGE_CACHE_KEY + news.getCategory_id() + "*");
        }
        return insertFlag;
    }

    @Override
    public boolean updateNews(News news) {
        if (news.getIstop() == 1) {
            news.setIstopupdatetime(new Date());
        }
        String cachekey = NewsInfoServiceImpl.NEWS_CACHE_KEY + news.getId();
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(NewsMapper.class, m -> m.updateNews(news));
        if (updateFlag) {
            JedisUtil.jedisDo(j -> {
                j.del(HOME_NEWS_CACHE_KEY);
                j.del(cachekey);
                return null;
            });
            JedisUtil.deleteKeys(NEWS_PAGE_CACHE_KEY + news.getCategory_id() + "*");
        }
        return updateFlag;
    }

    @Override
    public List<News> selectHomeNews() {
        List<News> list = null;
        try {
            String newsjson = JedisUtil.jedisDo(j -> j.get(HOME_NEWS_CACHE_KEY));
            if (StringUtils.isNotEmpty(newsjson)) {
                list = JsonTool.toCollection(newsjson, new TypeReference<List<News>>() {
                });
                JedisUtil.jedisDo(j -> j.expire(HOME_NEWS_CACHE_KEY, JedisUtil.CACHE_TIME));
            }
        } catch (Exception e) {
        }
        if (list == null) {
            list = SqlSessionFactoryTool.DoMapper(NewsMapper.class, m -> m.selectHomeNews());
            try {
                String newsjson = JsonTool.toJson(list);
                JedisUtil.jedisDo(j -> j.setex(HOME_NEWS_CACHE_KEY, JedisUtil.CACHE_TIME, newsjson));
            } catch (Exception e) {
            }
        }
        return list;
    }

    @Override
    public int selectNewsCountByCateId(int cate_id) {
        Integer icount = null;
        String cachekey = NEWS_PAGE_CACHE_KEY + cate_id;
        try {
            String count = JedisUtil.jedisDo(j -> j.get(cachekey));
            if (StringUtils.isNotEmpty(count)) {
                icount = Integer.parseInt(count);
            }
        } catch (Exception e) {
        }
        if (icount == null) {
            icount = SqlSessionFactoryTool.DoMapper(NewsMapper.class, m -> m.selectNewsCountByCateId(cate_id));
            String newsCount = icount.toString();
            try {
                JedisUtil.jedisDo(j -> j.setex(cachekey, JedisUtil.CACHE_TIME, newsCount));
            } catch (Exception e) {
            }
        }
        return icount;
    }

    @Override
    public List<News> selectNewsByCateId(int cate_id, Page page) {
        List<News> list = null;
        String cachekey = NEWS_PAGE_CACHE_KEY + cate_id + "_from_" + page.getSkip() + "_to_" + (page.getSkip() + page.getLimit());
        try {
            String newsListJson = JedisUtil.jedisDo(j -> j.get(cachekey));
            if (StringUtils.isNotEmpty(newsListJson)) {
                JedisUtil.jedisDo(j -> j.expire(cachekey, JedisUtil.CACHE_TIME));
                list = JsonTool.toCollection(newsListJson, new TypeReference<List<News>>() {
                });
            }
        } catch (Exception e) {
        }
        if (list == null) {
            list = SqlSessionFactoryTool.DoMapper(NewsMapper.class, m -> m.selectNewsByCateId(cate_id, page));
            try {
                String newsListJson = JsonTool.toJson(list);
                JedisUtil.jedisDo(j -> j.setex(cachekey, JedisUtil.CACHE_TIME, newsListJson));
            } catch (Exception e) {
            }
        }
        return list;
    }
}
